var DateUtil = require('DateUtil');
var AUTH     = require('AUTH');
module.exports = (function ($) {

    var NOOP = function () {
    };

    var URL_API_PREFIX = '/api';

    //get or post data
    function api_call(method, uri, data, cb_success, cb_error, cb_finally) {
        data = data || {};
        for (var i in data) {
            if (data.hasOwnProperty(i) && typeof data[i] === 'function') {
                delete data[i];
            }
            //avoid empty string problem
            if (data.hasOwnProperty(i) && data[i] === '') {
                delete data[i];
            }
        }
        if (!method) throw new Error("method is null");
        if (!uri) throw new Error("url is null");

        cb_success = (cb_success && typeof cb_success === 'function') ? cb_success : NOOP;
        cb_error = (cb_error && typeof cb_error === 'function') ? cb_error : function (msg, state) {
            alert("" + state + ":" + JSON.stringify(msg));
        };
        cb_finally = (cb_finally && typeof cb_finally === 'function') ? cb_finally : NOOP;

        $.ajax({
            type: method,
            url: URL_API_PREFIX + uri,
            data: data,
            headers: {
                Authorization: AUTH.getToken()
            },
            //dataType: "json",
            success: function (data, status, xhr) {
                cb_finally(cb_success(data, status, xhr));
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                var message = XMLHttpRequest.responseText;
                //var messageJsonMessage = XMLHttpRequest.responseJSON.message;
                var state = XMLHttpRequest.status;
                //console.log(XMLHttpRequest);
                //console.log(XMLHttpRequest);
                if (state >= 500) {
                    alert("服务器出错！" + message);
                }
                else if (state === 403) {
                    alert("拒绝访问! " + message);
                }
                else {
                    cb_error(message, state);
                }
                cb_finally();
            }
        });
    };

    var overlay = (function () {
        var html = "<div class='cover'><i class='fa fa-spinner fa-pulse'></i></div>";
        var $overlay = $(html);
        return {
            close: function () {
                $overlay.remove();
            },
            show: function () {
                this.close();
                $('body').prepend($overlay);
            }
        }
    })();

    //DAO --DATA ACCESS OBJECT
    return {

        rbac: {
            roles: function (cb_suc) {
                api_call('get', '/rbac/roles/', {}, cb_suc);
            },
            roles_for_user: function (user, cb_suc) {
                api_call('get', '/rbac/users/' + user + '/roles/', {}, cb_suc);
            },
            add_role_for_user: function (user, role, cb_suc, cb_err) {
                overlay.show();
                api_call('post', '/rbac/users/' + user + '/roles/', {"rid": role}, function (data) {
                    overlay.close();
                    cb_suc(data);
                }, cb_err);
            },
            remove_role_for_user: function (user, role, cb_suc) {
                overlay.show();
                api_call('delete', '/rbac/users/' + user + '/roles/' + role, {}, function (data) {
                    overlay.close();
                    cb_suc(data);
                })

            },
            reload: function (cb_suc) {
                api_call('get', '/rbac/reload', {}, cb_suc)
            }

        },

        sys: {
            login_user: function (cb_suc) {
                api_call('get', '/sys/login_user/', {}, cb_suc)
            },

            menu: function (cb_suc) {
                api_call('get', '/sys/menu/', {}, cb_suc)
            },

            recount_sales_vol: function (cb_suc, cb_err) {
                overlay.show();
                api_call('get', '/sys/recount_sales_vol', {}, cb_suc, cb_err, overlay.close);
            }

            //logout: function () {
            //    api_call('get', '/sys/logout/', {}, null, null);
            //
            //    var str = location.href.replace("http://", "http://" + new Date().getTime() + ":233321@");
            //    var xmlhttp;
            //    if (window.XMLHttpRequest) xmlhttp = new XMLHttpRequest();
            //    else xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            //    xmlhttp.onreadystatechange = function () {
            //        if (xmlhttp.readyState == 4) location.reload();
            //    };
            //    xmlhttp.open("GET", str, true);
            //    xmlhttp.setRequestHeader("Authorization", "Basic YXNkc2E6");
            //    xmlhttp.send();
            //    return false;
            //
            //}
        },

        shop: {

            all: function (cb_suc, cb_err) {
                overlay.show();
                api_call('get', '/shops/', {}, cb_suc, cb_err, overlay.close);
            },
            all_normal: function (cb_suc, cb_err) {
                overlay.show();
                api_call('get', '/shops/all/normal', {}, cb_suc, cb_err, overlay.close);
            },
            add: function (add, cb_suc) {
                api_call('post', '/shops/', add, cb_suc)
            },
            get: function (id, cb_suc, cb_err) {
                api_call('get', '/shops/' + id, {}, cb_suc, cb_err)
            },
            upd: function (shop, cb_suc, cb_err) {
                api_call('put', '/shops/' + shop.id, shop, cb_suc, cb_err)
            },
            del: function (shop, cb_suc, cb_err) {
                api_call('delete', '/shops/' + shop.id, {}, cb_suc, cb_err)
            },
            sort: function (qo, cb_suc, cb_err) {
                api_call('put', '/shops/sort', qo, cb_suc, cb_err)
            }
        },

        user: {
            get: function (id, cb_suc, cb_err) {
                api_call('get', '/users/' + id, {}, cb_suc, cb_err)
            },
            all: function (cb_suc, cb_err) {
                api_call('get', '/users/', {}, cb_suc, cb_err)
            },
            add: function (user, cb_suc, cb_err) {
                api_call('post', '/users/', user, cb_suc, cb_err)
            },
            upd: function (user, cb_suc, cb_err) {
                api_call('put', '/users/' + user.uid, user, cb_suc, cb_err)
            },
            del: function (uid, cb_suc) {
                if (uid) api_call('delete', '/users/' + uid, {}, cb_suc)
            },
            brief: function (cb_suc, cb_err) {
                api_call('get', '/brief/users', {}, cb_suc, cb_err)
            },
            salesmen: function (shop_id, cb_suc) {
                api_call('get', '/users/salesmen', shop_id ? {shop_id: shop_id} : {}, cb_suc)
            },
            evaluator: function (shop_id, cb_suc) {
                api_call('get', '/users/evaluator', shop_id ? {shop_id: shop_id} : {}, cb_suc)
            }
        },

        shop_user: {
            remove_user_from_shop: function (user, cb_suc) {
                api_call('delete', '/users/' + user.uid + '/shop', {}, cb_suc);
            },
            add_user_to_shop: function (user, shop, cb_suc) {
                api_call('post', '/users/' + user.uid + '/shop', {shop_id: shop.id}, cb_suc);
            },
            users_without_shop: function (cb_suc) {
                api_call('get', '/users/shop/null', {}, cb_suc);
            },
            users_of_shop: function (shop_id, cb_suc) {
                api_call('get', '/shops/' + shop_id + '/users', {}, cb_suc);
            }
        },
        rank: {
            all: function (cb_suc, cb_err) {
                overlay.show();
                api_call('get', '/ranks/', {}, cb_suc, cb_err, overlay.close);
            },

            get: function (id, cb_suc, cb_err) {
                if (id) {
                    overlay.show();
                    api_call('get', '/ranks/' + id, {}, cb_suc, cb_err, overlay.close)
                }
            },

            add: function (rank, cb_suc, cb_err) {
                overlay.show();
                api_call('post', '/ranks/', rank, cb_suc, cb_err, overlay.close)
            },

            upd: function (rank, cb_suc, cb_err) {
                if (rank.id) {
                    overlay.show();
                    api_call('put', '/ranks/' + rank.id, rank, cb_suc, cb_err, overlay.close)
                }
            },

            del: function (id, cb_suc, cb_err) {
                if (id) {
                    overlay.show();
                    api_call('delete', '/ranks/' + id, {}, cb_suc, cb_err, overlay.close)
                }
            },

            roles_for_rank: function (rank, cb_suc, cb_err) {
                if (rank.id) {
                    overlay.show();
                    api_call('get', '/ranks/' + rank.id + '/roles/', {}, cb_suc, cb_err, overlay.close)
                }
            },

            add_role_to_rank: function (role, rank, cb_suc, cb_err) {
                if (role.id && rank.id) {
                    overlay.show();
                    api_call('post', '/ranks/' + rank.id + '/roles/' + role.id, {}, cb_suc, cb_err, overlay.close)
                }
            },

            remove_role_from_rank: function (role, rank, cb_suc) {
                if (role.role_id && rank.id) {
                    api_call('delete', '/ranks/' + rank.id + '/roles/' + role.role_id, {}, cb_suc)
                }
            },

            ranks_for_user: function (user, cb_suc, cb_err) {
                if (user.id) {
                    overlay.show();
                    api_call('get', '/users/' + user.id + '/ranks/', {}, cb_suc, cb_err, overlay.close)
                }
            },

            add_rank_to_user: function (rank, user, cb_suc, cb_err) {
                if (rank && user.id) {
                    overlay.show();
                    api_call('post', '/users/' + user.id + '/ranks/' + rank.id, {}, cb_suc, cb_err, overlay.close)
                }
            },

            remove_rank_from_user: function (rank, user, cb_suc, cb_err) {
                if (rank.id && user.id) {
                    overlay.show();
                    api_call('delete', '/users/' + user.id + '/ranks/' + rank.id, {}, cb_suc, cb_err, overlay.close)
                }
            }

        },
        after_sale:require('API_AFTER_SALE')(api_call, overlay)

    }

})(jQuery);